package com.example.coursemanagement.controller;


import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.MyUtils.ResultCode;
import com.example.coursemanagement.mapper.TeacherMapper;
import com.example.coursemanagement.pojo.Teacher;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangmenghen
 * @since 2020-06-01
 */

//没问题
@RestController
@RequestMapping("/teacher" )
@RequiredArgsConstructor
public class TeacherController{

    private final TeacherMapper teacherMapper;

    @ApiOperation("查询所有教师信息")
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public CommonResult<Object> queryAllTeacher(){
        Collection<Teacher> teachers = teacherMapper.selectList(null);
        if(teachers==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(teachers);
        }
    }

    @ApiOperation("新增操作")
    @RequestMapping(value = "/addTeacher",method = RequestMethod.POST)
    public CommonResult<Object> addTeacher(Teacher teacher){
        if(teacherMapper.insert(teacher)>0){
            return CommonResult.success(ResultCode.ADD_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("修改操作")
    @RequestMapping(value = "/updateTeacher",method = RequestMethod.POST)
    public CommonResult<Object> updateTeacher(Teacher teacher){
        if(teacherMapper.updateById(teacher)>0){
            return CommonResult.success(ResultCode.UPDATE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("删除操作")
    @RequestMapping(value = "/deleteTeacher",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteTeacher(@Param("id") int id){
        if(teacherMapper.deleteById(id)>0){
            return CommonResult.success(ResultCode.DELETE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

}

